﻿using APP.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace APP.Windows
{

    public class ScreenHelper
    {

        public List<ScreenInfo> GetScreenInfos()
        {
            var ScreenInfos = new List<ScreenInfo>();
            foreach (var tempScreen in System.Windows.Forms.Screen.AllScreens)
            {
                var bitmap = CaptureScreen(tempScreen.Bounds.X, tempScreen.Bounds.Y, tempScreen.Bounds.Width, tempScreen.Bounds.Height);
                var tempScreenInfo = new ScreenInfo();
                tempScreenInfo.Screen = tempScreen;
                tempScreenInfo.ScreenImage = bitmap;
                tempScreenInfo.Scale = Win32Helper.GetCurrentDpiScale();
                double Dpirate = ScreenInformations.GetDPIRate(tempScreen.Bounds.X, tempScreen.Bounds.Y);
                tempScreenInfo.pixDpi = Dpirate;
                ScreenInfos.Add(tempScreenInfo);
            }
            return ScreenInfos;
        }

        private bool CursorInScreen(System.Windows.Forms.Screen screen, double x, double y)
        {
            return screen.Bounds.Left < x
               && screen.Bounds.Right > x
               && screen.Bounds.Top < y
               && screen.Bounds.Bottom > y;
        }


        public static System.Drawing.Bitmap CapturePrimaryScreen()
        {
            var screen = System.Windows.Forms.Screen.AllScreens.First(x => x.Primary);
            return CaptureScreen(screen.Bounds.X, screen.Bounds.Y, screen.Bounds.Width, screen.Bounds.Height);
        }

        public static System.Drawing.Rectangle CapturePrimaryScreenRectangle()
        {
            var screen = System.Windows.Forms.Screen.AllScreens.First(x => x.Primary);
            return screen.Bounds;
        }

        public static System.Drawing.Bitmap CaptureScreen(System.Windows.Forms.Screen screen)
        {
            return CaptureScreen(screen.Bounds.X, screen.Bounds.Y, screen.Bounds.Width, screen.Bounds.Height);
        }
        public static System.Drawing.Bitmap CaptureScreen(System.Drawing.Rectangle rect)
        {
            return CaptureScreen(rect.X, rect.Y, rect.Width, rect.Height);
        }
        /// <summary>
        /// 截取屏幕
        /// </summary>
        /// <param name="x">起点X坐标</param>
        /// <param name="y">起点Y坐标</param>
        /// <param name="width">截取宽度</param>
        /// <param name="height">截取高度</param>
        /// <returns></returns>
        private static System.Drawing.Bitmap CaptureScreen(double x, double y, double width, double height)
        {
            int ix = Convert.ToInt32(x);
            int iy = Convert.ToInt32(y);
            int iw = Convert.ToInt32(width);
            int ih = Convert.ToInt32(height);

            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(iw, ih);
            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);
            int index = 0;
            while (true)
            {
                try
                {
                    graphics.CopyFromScreen(ix, iy, 0, 0, new System.Drawing.Size(iw, ih));
                    break;
                }
                catch (Exception ex)
                {
                    GC.Collect();
                    index++;
                    if (index > 3)
                    {
                        throw new Exception($"截取全屏失败");
                    }
                }
            }
            graphics.Dispose();
            return bitmap;
        }


    }
}
